﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using NHibernate.Criterion;
using Rodunu.Movies.Contracts;
using Rodunu.Movies.Data;
using Rodunu.Movies.Interfaces.Movie;

namespace Rodunu.Movies.Logic.Movie
{
    public class ServicesMovieLinkLogic : Repository<Contracts.ServicesMovieLink>, IServicesMovieLink
    {
        public IList<ServicesMovieLink> GetServiceMoviesByUserAndMovie(long userId, long movieId)
        {
            ISession session = DBHelper.GetCurrentSession();
            return session.CreateCriteria(typeof(Contracts.ServicesMovieLink))
                                                     .Add(Expression.Eq("UserInfo.Id", userId))
                                                     .Add(Expression.Eq("Movie.Id", movieId))
                                                     .List<ServicesMovieLink>();
        }

        public IList<ServicesMovieLink> GetServiceMoviesByMovie(long movieId)
        {
            ISession session = DBHelper.GetCurrentSession();
            return session.CreateCriteria(typeof(Contracts.ServicesMovieLink))
                                                     .Add(Expression.Eq("Movie.Id", movieId))
                                                     .List<ServicesMovieLink>();
        }

        public ServicesMovieLink GetMostUsedServiceMovieByMovie(long movieId)
        {
            ISession session = DBHelper.GetCurrentSession();
            IQuery query = session.GetNamedQuery("GetMostUsedServiceLinks");
            query.SetInt64("MovieId", movieId);
            return (ServicesMovieLink)query.UniqueResult();
        }
    }
}
